import UIKit

class AboutUsViewController: UIViewController {
    
    // MARK: - UI Components
    private let headerView = UIView()
    private let titleLabel = UILabel()
    private let backButton = UIButton(type: .system)
    
    private let scrollView = UIScrollView()
    private let contentView = UIView()
    
    private let appIconImageView = UIImageView()
    private let appNameLabel = UILabel()
    private let versionLabel = UILabel()
    private let descriptionLabel = UILabel()
    private let featuresLabel = UILabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        setupConstraints()
        loadAppInfo()
    }
    
    private func setupUI() {
        view.backgroundColor = .white
        title = "关于我们"
        
        setupHeaderView()
        
        // 设置滚动视图
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        
        // 设置应用图标
        appIconImageView.image = UIImage.init(named: "about_icon")
        appIconImageView.contentMode = .scaleAspectFit
        appIconImageView.layer.cornerRadius = 20
        appIconImageView.clipsToBounds = true
        
        // 设置应用名称
        appNameLabel.font = UIFont.boldSystemFont(ofSize: 24)
        appNameLabel.textColor = .black
        appNameLabel.textAlignment = .center
        
        // 设置版本号
        versionLabel.font = UIFont.systemFont(ofSize: 16)
        versionLabel.textColor = .gray
        versionLabel.textAlignment = .center
        
        // 设置应用描述
        descriptionLabel.font = UIFont.systemFont(ofSize: 16)
        descriptionLabel.textColor = .black
        descriptionLabel.numberOfLines = 0
        descriptionLabel.textAlignment = .center
        descriptionLabel.lineBreakMode = .byWordWrapping
        
        // 设置功能特点
        featuresLabel.font = UIFont.systemFont(ofSize: 14)
        featuresLabel.textColor = .darkGray
        featuresLabel.numberOfLines = 0
        featuresLabel.textAlignment = .left
        featuresLabel.lineBreakMode = .byWordWrapping
        
        // 添加到视图层级
        headerView.addSubview(titleLabel)
        headerView.addSubview(backButton)
        view.addSubview(headerView)
        
        contentView.addSubview(appIconImageView)
        contentView.addSubview(appNameLabel)
        contentView.addSubview(versionLabel)
        contentView.addSubview(descriptionLabel)
        contentView.addSubview(featuresLabel)
        
        scrollView.addSubview(contentView)
        view.addSubview(scrollView)
    }
    
    private func setupHeaderView() {
        titleLabel.text = "关于我们"
        titleLabel.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
        titleLabel.textColor = .black
        titleLabel.textAlignment = .center
        
        backButton.setImage(UIImage(systemName: "chevron.left"), for: .normal)
        backButton.tintColor = .black
        backButton.addTarget(self, action: #selector(backButtonTapped), for: .touchUpInside)
        
        headerView.backgroundColor = .clear
    }
    
    private func setupConstraints() {
        headerView.translatesAutoresizingMaskIntoConstraints = false
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        backButton.translatesAutoresizingMaskIntoConstraints = false
        
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        contentView.translatesAutoresizingMaskIntoConstraints = false
        appIconImageView.translatesAutoresizingMaskIntoConstraints = false
        appNameLabel.translatesAutoresizingMaskIntoConstraints = false
        versionLabel.translatesAutoresizingMaskIntoConstraints = false
        descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
        featuresLabel.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            // Header
            headerView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10),
            headerView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
            headerView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
            headerView.heightAnchor.constraint(equalToConstant: 44),
            
            backButton.leadingAnchor.constraint(equalTo: headerView.leadingAnchor),
            backButton.centerYAnchor.constraint(equalTo: headerView.centerYAnchor),
            backButton.widthAnchor.constraint(equalToConstant: 44),
            backButton.heightAnchor.constraint(equalToConstant: 44),
            
            titleLabel.centerXAnchor.constraint(equalTo: headerView.centerXAnchor),
            titleLabel.centerYAnchor.constraint(equalTo: headerView.centerYAnchor),
            
            // ScrollView
            scrollView.topAnchor.constraint(equalTo: headerView.bottomAnchor, constant: 20),
            scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            
            // ContentView
            contentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
            contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
            contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
            contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
            contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor),
            
            // App Icon
            appIconImageView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 40),
            appIconImageView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
            appIconImageView.widthAnchor.constraint(equalToConstant: 100),
            appIconImageView.heightAnchor.constraint(equalToConstant: 100),
            
            // App Name
            appNameLabel.topAnchor.constraint(equalTo: appIconImageView.bottomAnchor, constant: 20),
            appNameLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
            appNameLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20),
            
            // Version
            versionLabel.topAnchor.constraint(equalTo: appNameLabel.bottomAnchor, constant: 8),
            versionLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
            versionLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20),
            
            // Description
            descriptionLabel.topAnchor.constraint(equalTo: versionLabel.bottomAnchor, constant: 30),
            descriptionLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 30),
            descriptionLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -30),
            
            // Features
            featuresLabel.topAnchor.constraint(equalTo: descriptionLabel.bottomAnchor, constant: 30),
            featuresLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 30),
            featuresLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -30),
            featuresLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -40)
        ])
    }
    
    private func loadAppInfo() {
        // 设置应用图标
        if let appIcon = UIImage(named: "about_icon") {
            appIconImageView.image = appIcon
        } else {
            // 如果没有找到AppIcon，使用默认图标
            appIconImageView.image = UIImage(systemName: "app.fill")
            appIconImageView.tintColor = .systemBlue
        }
        
        // 设置应用名称
        appNameLabel.text = "智能扫描仪"
        
        // 设置版本号
        if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String,
           let build = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
            versionLabel.text = "版本 \(version) (\(build))"
        } else {
            versionLabel.text = "版本 1.0"
        }
        
        // 设置应用描述
        descriptionLabel.text = "一款功能强大的智能扫描应用，致力于为用户提供便捷的文档处理体验。我们专注于OCR识别、文档扫描、AI翻译、手机测距等核心功能，让您的文档处理更加高效。"
        
        // 设置功能特点
        featuresLabel.text = """
        主要功能：
        
        📱 OCR文字识别
        快速识别图片中的文字，支持多种语言
        
        📄 文档扫描
        智能文档扫描，自动优化图片质量
        
        🤖 AI翻译
        智能翻译功能，支持多种语言互译
        
        📏 手机测距
        AR技术实现精准距离测量
        
        🖼️ 老照片修复
        智能修复和优化老照片
        
        我们致力于为用户提供最好的使用体验！
        """
    }
    
    // MARK: - Lifecycle Methods
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        // 隐藏底部标签栏和系统导航栏
        navigationController?.setNavigationBarHidden(true, animated: false)
        navigationController?.tabBarController?.tabBar.isHidden = true
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        // 恢复底部标签栏和系统导航栏
        navigationController?.setNavigationBarHidden(false, animated: false)
        navigationController?.tabBarController?.tabBar.isHidden = false
    }
    
    // MARK: - Actions
    @objc private func backButtonTapped() {
        navigationController?.popViewController(animated: true)
    }
} 
